package com.run.wz.center.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * Description: 分组枚举
 * Copyright (c) Department of Research and Development/Beijing/Run.
 * All Rights Reserved.
 * @version 1.0  2012-6-6 下午11:34:53  by 张红意（hongyi@run.com）创建
 */
public enum GroupType {
	
	
	GROUP1(1){

		@Override
		public List<ProtocolType> getProtocolTypes() {
			List<ProtocolType> protocolTypes = new ArrayList<ProtocolType>();
			for(ProtocolType protocolType : ProtocolType.values()){
				if(this.equals(protocolType.getGroupTyp())){
					protocolTypes.add(protocolType);
				}
			}
			return protocolTypes;
		}

		
	},GROUP3(3){

		@Override
		public List<ProtocolType> getProtocolTypes() {
			List<ProtocolType> protocolTypes = new ArrayList<ProtocolType>();
			for(ProtocolType protocolType : ProtocolType.values()){
				if(this.equals(protocolType.getGroupTyp())){
					protocolTypes.add(protocolType);
				}
			}
			return protocolTypes;
		}

	};

	private GroupType(int groupTypeNum) {
		this.groupTypeNum = groupTypeNum;
	}
	
	private final int groupTypeNum;

	public int getNum() {
		return this.groupTypeNum;
	}
	public static GroupType getGroupTypeByNum(int num) {
		for(GroupType pt : GroupType.values()) {
			if(pt.getNum()==num) return pt;
		}
		return null;
	}
	/**
	 * 根据协议数组获取获取组列表
	 * @param pts
	 * @return
	 */
	public static ArrayList<String> getGroupListByPTArray(ProtocolType[] pts) {
		ArrayList<String> groups = new ArrayList<String>();
		for(GroupType gt : GroupType.values()){
			for(ProtocolType protocol : pts){
				if(gt.getProtocolTypes().contains(protocol)){
					String group = String.valueOf(gt.getNum());
					if(!groups .contains(group)){
						groups.add(group);
					}
				}
			}
		}
		return groups;
	}
	
	/**
	 * 根据协议集合获得组字符串集合
	 * chenxu 2012-9-6 上午11:03:20
	 * @param pts
	 * @return
	 */
	public static TreeSet<String> getGroupListByPTSet(TreeSet<ProtocolType> pts) {
		TreeSet<String> groups = new TreeSet<String>();
		for(GroupType gt : GroupType.values()){
			for(ProtocolType protocol : pts){
				if(gt.getProtocolTypes().contains(protocol)){
					String group = String.valueOf(gt.getNum());
					if(!groups .contains(group)){
						groups.add(group);
					}
				}
			}
		}
		return groups;
	}
	/**
	 * 将协议集合按照“组”分组
	 * chenxu 2012-9-6 上午11:11:57
	 * @param pts
	 * @return
	 */
	public static TreeMap<String, TreeSet<ProtocolType>> groupPTSetByGroup(TreeSet<ProtocolType> pts) {
		TreeMap<String, TreeSet<ProtocolType>> map = new TreeMap<String, TreeSet<ProtocolType>>();
		for(GroupType gt : GroupType.values()){
			for(ProtocolType protocol : pts){
				if(gt.getProtocolTypeSet().contains(protocol)){
					String group = String.valueOf(gt.getNum());
					TreeSet<ProtocolType> treeSet = map.get(group);
					if(null==treeSet) {
						treeSet = new TreeSet<ProtocolType>();
					}
					treeSet.add(protocol);
					map.put(group, treeSet);
				}
			}
		}
		return map;
	}
	
	/**
	 * Description: 获取当前分组下的协议集合
	 * @Version1.0 2012-7-3 下午10:52:14 by 张红意（hongyi@run.com）创建
	 * @return
	 */
	public abstract List<ProtocolType> getProtocolTypes();
	/**
	 * 获得当前组的协议集合
	 * chenxu 2012-9-6 上午10:50:13
	 * @return
	 */
	public TreeSet<ProtocolType> getProtocolTypeSet() {
		TreeSet<ProtocolType> protocolTypes = new TreeSet<ProtocolType>();
		for(ProtocolType protocolType : ProtocolType.values()){
			if(this.equals(protocolType.getGroupTyp())){
				protocolTypes.add(protocolType);
			}
		}
		return protocolTypes;
	}
}
